//
//  CalendarTool.m
//  Daybook
//
//  Created by dllo on 16/5/21.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "CalendarTool.h"
#import "Common.h"

@interface CalendarTool ()

@property (nonatomic, strong)RLMRealm *realm;

@property (nonatomic, strong)RLMResults *accountBooks;

@end
@implementation CalendarTool

+ (CalendarTool *)shareCalendarTool {
    static CalendarTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[CalendarTool alloc] init];
    });
    return tool;
}

/*********************       扩展方法   *********************************/

- (NSMutableArray *)arrayMonthWeekendFromDate:(NSDate *)date {
    NSDate *anyDate = date;
    NSInteger week = [self firstWeekdayInThisMonth:anyDate];
    NSInteger countDay = [self totaldaysInMonth:anyDate];
    
    NSMutableArray *firstWeekDay = [NSMutableArray array];
    NSMutableArray *endWeekDay = [NSMutableArray array];
    NSMutableArray *inputArr = [NSMutableArray array];
    
    NSInteger endFirstDay = (7 - week) + 1;
    
    NSInteger month = [self month:anyDate];
    
    [firstWeekDay addObject:[NSString stringWithFormat:@"%02ld.01", month]];
    
    if (week == 0) {
        [endWeekDay addObject:[NSString stringWithFormat:@"%02ld.01", month]];
        [firstWeekDay addObject:[NSString stringWithFormat:@"%02ld.02", month]];
    }
    
    for (NSInteger i = endFirstDay + 1; i <= countDay; i += 7) {
        [firstWeekDay addObject:[NSString stringWithFormat:@"%02ld.%02ld", month,i]];
        [endWeekDay addObject:[NSString stringWithFormat:@"%02ld.%02ld", month, i - 1]];
        
    }
    
    NSString *firstLastNum = [firstWeekDay lastObject];
    NSString *endLastNum = [endWeekDay lastObject];
    
    if (countDay > firstLastNum.integerValue) {
        [endWeekDay addObject:[NSString stringWithFormat:@"%02ld.%02ld", month,countDay]];
    }
    
    if (endLastNum.integerValue + 1 == firstLastNum.integerValue && countDay == firstLastNum.integerValue) {
        [endWeekDay addObject:[NSString stringWithFormat:@"%02ld.%02ld", month, firstLastNum.integerValue]];
    }
    
    
    for (NSInteger i = 0; i < firstWeekDay.count; i++) {
        NSString *firstStr = firstWeekDay[i];
        NSString *endStr = endWeekDay[i];
        [inputArr addObject:[NSString stringWithFormat:@"%@-%@", firstStr, endStr]];
    }
    return inputArr;
}

- (NSString *)weekStringFromDate:(NSDate *)date {
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
    
    return [weekdays objectAtIndex:theComponents.weekday];
}



/*********************     其它date方法   *********************************/
//返回截止到该日期时有几天
- (NSInteger)day:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components day];
}

//第几月
- (NSInteger)month:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components month];
}

//年份
- (NSInteger)year:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components year];
}

//这个月的第一天是周几
- (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
    NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    [comp setDay:1];
    NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
    
    NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
    return firstWeekday - 1;
}



//这个月有几天
- (NSInteger)totaldaysInMonth:(NSDate *)date{
    NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return daysInLastMonth.length;
}

//上个月的的时间
- (NSDate *)lastMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = -1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

//下一个月的时间
- (NSDate*)nextMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = +1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

//通过数字返回星期几
- (NSString *)getWeekStringFromInteger:(NSInteger)week
{
    NSString *str_week;
    
    switch (week) {
        case 1:
            str_week = @"周日";
            break;
        case 2:
            str_week = @"周一";
            break;
        case 3:
            str_week = @"周二";
            break;
        case 4:
            str_week = @"周三";
            break;
        case 5:
            str_week = @"周四";
            break;
        case 6:
            str_week = @"周五";
            break;
        case 7:
            str_week = @"周六";
            break;
    }
    return str_week;
}

- (NSInteger)getIndexFromDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
    return theComponents.weekday;
}


/********************     date与string转换   ****************************/

//
//NSString转NSDate
- (NSDate *)dateFromString:(NSString *)dateString
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    
    return destDate;
    
}



//NSDate转NSString
- (NSString *)stringFromDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    
    //    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}

- (NSString *)detailStringFromDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy.MM.dd HH:mm"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
}

/***********************************************************************/

- (NSDate *)firstDayFromDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *beginDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%04ld-%02ld-01 00:00:00", [self year:date], [self month:date]]];
    return beginDate;
}

- (NSDate *)lastDayFromDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *lastDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%04ld-%02ld-%02ld 23:59:59", [self year:date], [self month:date], [self totaldaysInMonth:date]]];
    return lastDate;
}


/***********************************************************************/

- (NSDate *)beginDateFromSmallStringDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *beginDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 00:00:00", [self stringFromDate:date]]];
    return beginDate;
}

- (NSDate *)endDateFromSmallStringDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *endDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ 23:59:59", [self stringFromDate:date]]];
    return endDate;
}

- (NSMutableArray *)AccountBookModelArrayFromSmallDate:(NSDate *)date {
    NSMutableArray *tempArr = [NSMutableArray array];
    NSDate *endDate = [[CalendarTool shareCalendarTool] endDateFromSmallStringDate:date];
    NSDate *beginDate = [[CalendarTool shareCalendarTool] beginDateFromSmallStringDate:date];
    
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    
    RLMResults *lookResult = [currentDayBook.bookContent objectsWhere:@"time >  %@ and time < %@", beginDate, endDate];
    tempArr = (NSMutableArray *)lookResult;
    return tempArr;
}


- (NSString *)getMonthAndDayStringFromDate:(NSDate *)date {
    NSString *str = [self stringFromDate:date];
    return [str substringFromIndex:5];
}

@end
